package org.example.common.exception;

import org.example.common.response.ResponseData;
import org.example.common.response.ResponseEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class GlobalExceptionAdivce {
    /**
     * 处理全局的SQLException
     */
    @ExceptionHandler(SQLException.class)
    public ResponseData handleSqlException(SQLException e){ return ResponseData.error(ResponseEnum.DB_ERROR);}//返回全局定义的业务异常
    @ExceptionHandler(CustomException.class)//CustomException是指自定义错误，是程序员编写程序代码的错误而不是其他原因
    public ResponseData handleCustomException(CustomException e){return ResponseData.error(e.getResponseEnum());}
    @ExceptionHandler(Exception.class)//用于处理全局的其他Exception错误
    public ResponseData handleException(Exception e){return ResponseData.error(ResponseEnum.SERVER_ERROR);}
}
